import * as vscode from 'vscode';

export async function fileExists(uri: vscode.Uri): Promise<boolean> {
  try {
    await vscode.workspace.fs.stat(uri);
    return true;
  } catch {
    return false;
  }
}

export async function readSettingsFile(uri: vscode.Uri): Promise<Record<string, any>> {
  try {
    const data = await vscode.workspace.fs.readFile(uri);
    const text = new TextDecoder().decode(data);
    return JSON.parse(text);
  } catch (e) {
    console.error("无法读取 settings.json", e);
    return {};
  }
}

export async function writeSettingsFile(uri: vscode.Uri, settings: Record<string, any>) {
  const content = JSON.stringify(settings, null, 2);
  await vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(content));
}